package com.binguo.model.mapper;

import com.binguo.model.api.vo.*;
import com.binguo.model.core.Mapper;
import com.binguo.model.model.BinguoOrder;
import com.binguo.model.model.Customer;
import com.binguo.model.model.StatisticOrder;
import com.binguo.model.mybatis.DomainExample;

import com.binguo.model.wxapi.form.ApiOrderListForm;
import com.binguo.model.wxapi.vo.ReserveOrderVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;

import java.util.List;

public interface BinguoOrderMapper extends Mapper<BinguoOrder> {
    @Select("SELECT * FROM binguo_order WHERE shopId = #{shopId} and cashierDeskId = #{cashierDeskId} and payStatus != 0 and orderType = 1 ORDER BY id DESC Limit #{size}")
    List<BinguoOrder> findBySize(@Param("shopId")Integer shopId, @Param("cashierDeskId")Integer cashierDeskId, @Param("size")Integer size);

    @Select("SELECT * FROM binguo_order WHERE shopId = #{shopId} and cashierDeskId = #{cashierDeskId} and orderType = 1 and payStatus = 1 and refundStatus = 0 ORDER BY id DESC Limit #{size}")
    List<BinguoOrder> findPaySuccessBySize(@Param("shopId")Integer shopId, @Param("cashierDeskId")Integer cashierDeskId, @Param("size")Integer size);

    @Select("SELECT * FROM binguo_order WHERE shopId = #{shopId} and cashierDeskId = #{cashierDeskId} and orderType = 1 and payStatus = 1 and refundStatus = 3 ORDER BY id DESC Limit #{size}")
    List<BinguoOrder> findRefundSuccessBySize(@Param("shopId")Integer shopId, @Param("cashierDeskId")Integer cashierDeskId, @Param("size")Integer size);


    @Select("SELECT * FROM binguo_order WHERE shopId = #{shopId} and cashierDeskId = #{cashierDeskId} and orderType = 1 and id < #{orderId} ORDER BY id DESC limit #{size};")
    List<BinguoOrder> findByLastOrderNo(@Param("shopId")Integer shopId, @Param("cashierDeskId")Integer cashierDeskId, @Param("orderId")Integer orderId, @Param("size")Integer size);

    @Select("SELECT * FROM binguo_order WHERE shopId = #{shopId} and cashierDeskId = #{cashierDeskId} and orderType = 1 and payStatus = 1 and refundStatus = 0 and id < #{orderId} ORDER BY id DESC limit #{size};")
    List<BinguoOrder> findPaySuccessByLastOrderNo(@Param("shopId")Integer shopId, @Param("cashierDeskId")Integer cashierDeskId, @Param("orderId")Integer orderId, @Param("size")Integer size);

    @Select("SELECT * FROM binguo_order WHERE shopId = #{shopId} and cashierDeskId = #{cashierDeskId} and orderType = 1 and payStatus = 1 and refundStatus = 3 and id < #{orderId} ORDER BY id DESC limit #{size};")
    List<BinguoOrder> findRefundSuccessByLastOrderNo(@Param("shopId")Integer shopId, @Param("cashierDeskId")Integer cashierDeskId, @Param("orderId")Integer orderId, @Param("size")Integer size);


    @Select("SELECT * FROM binguo_order WHERE customerId = #{customerId} and payStatus = 1 and orderType in (1,4) ORDER BY id DESC Limit #{size}")
    List<BinguoOrder> findWXBySize(@Param("customerId")Integer customerId, @Param("size")Integer size);

    @Select("SELECT * FROM binguo_order WHERE customerId = #{customerId} and orderType in (1,4) and payStatus = 1 and id < #{orderId} ORDER BY id DESC limit #{size};")
    List<BinguoOrder> findWXByLastOrderNo(@Param("customerId")Integer customerId, @Param("orderId")Integer orderId, @Param("size")Integer size);

    @Select("select o.* from binguo_order as o join order_item as i on o.orderNo = i.orderNo where (i.productName like '%' #{searchKey} '%' or o.orderNo like '%' #{searchKey} '%') and o.customerId = #{customerId} and o.payStatus = 1 and orderType in (1,4) group by o.orderNo ORDER BY o.id DESC Limit #{size}")
    List<BinguoOrder> findWXBySizeAndSearchKey(@Param("searchKey")String searchKey, @Param("customerId")Integer customerId, @Param("size")Integer size);

    @Select("select o.* from binguo_order as o join order_item as i on o.orderNo = i.orderNo where (i.productName like '%' #{searchKey} '%' or o.orderNo like '%' #{searchKey} '%') and o.customerId = #{customerId} and o.id < #{orderId} and o.payStatus = 1 and orderType in (1,4) group by o.orderNo ORDER BY o.id DESC Limit #{size}")
    List<BinguoOrder> findWXByLastOrderNoAndSearchKey(@Param("searchKey")String searchKey, @Param("customerId")Integer customerId, @Param("orderId")Integer orderId, @Param("size")Integer size);

    @Select("SELECT * FROM binguo_order WHERE cardId = #{cardId} and orderType = 2 and (payStatus = 1 or refundStatus = 3) ORDER BY id DESC")
    List<BinguoOrder> findPayCardOrder(@Param("cardId")Integer cardId);

    @Select("SELECT COUNT(*) as count,COALESCE(SUM(totalFee),0) as sum FROM binguo_order WHERE shopId = 74 AND payStatus = 1 AND orderType = 3 AND created BETWEEN #{startTime}  AND #{endTime}")
    YizhifuBinguoOrderVO findYizhifuOrder(@Param("startTime")String startTime, @Param("endTime")String endTime);

    @Select("SELECT * FROM binguo_order WHERE orderNo LIKE '%SM' AND cashierDeskId = #{cashierDeskId} AND payStatus = 1 ORDER BY payTime DESC LIMIT #{limit} ,20")
    List<BinguoOrder> findShangMiOrder(@Param("limit") Integer limit,@Param("cashierDeskId") Integer cashierDeskId);


    /**
     */
    @Select("<script> \n " +
            " SELECT *  " +
            " FROM binguo_order" +
            " where payStatus = 1 " +
            " <if test='domain.shopid != null'> and shopId = '${domain.shopid}' </if> " +
            " order by id desc " +
            " <if test='null != addition'> " +
            " limit #{addition.offset}, #{addition.limit} " +
            " </if> " +
            " </script> ")
    List<BinguoOrder> findList(DomainExample<BinguoOrder> example);

    /**
     */
    @Select("<script> \n" +
            " SELECT count(*)  " +
            " FROM binguo_order" +
            " where payStatus = 1 " +
            " <if test='domain.shopid != null'> and shopId = '${domain.shopid}' </if> " +
            " </script>"
    )
    Integer findOrderCount(DomainExample<BinguoOrder> example);

   List<SMStatisticsOrderVO> getSMStatistics(@Param("deskId") Integer deskId,
                                             @Param("startTime")String startTime,
                                             @Param("endTime")String endTime);


    List<BinguoOrder> findMealOrder(@Param("searchKey")String searchKey,
                                    @Param("shopId")Integer shopId,
                                    @Param("branchId") Integer branchId,
                                    @Param("orderId")Integer orderId,
                                    @Param("size")Integer size,
                                    @Param("isMeal") Integer isMeal);

    @Select("SELECT * FROM binguo_order where orderNo = #{orderNo};")
    BinguoOrder findByOrderNo(@Param("orderNo") String orderNo);

    BinguoOrderVO findIsMealByOrderNo(@Param("orderNo") String orderNo);

    @Select("select * from binguo_order where orderNo = #{orderNo}")
    BinguoOrder findOrderByOrderNo(@Param("orderNo")String orderNo);

    @Select("<script>select binguo_order.orderNo as orderNo,binguo_order.product_order_url as productOrderUrl,binguo_order.customerId as customerId,binguo_order.customerName as customerName,binguo_order.customerPhone as customerPhone,binguo_order.source as payType,binguo_order.orderType as orderType,binguo_order.totalFee as totalFee,binguo_order.realFee as realFee,binguo_order.payTime  as payTime,binguo_order.refundFee as refundFee,binguo_order.payStatus as payStatus,binguo_order.refundStatus as refundStatus from binguo_order where shopId = #{shopId} <if test='order.payTime !=null '> and  binguo_order.payTime > #{order.payTime}  </if> <if test=' order.userName !=  null '> and binguo_order.customerId in (select customer.id from customer where customer.`name` like '%${order.userName}%') </if> limit ${order.page}, 20 </script>")
    List<BinguoOrder> findOrders(@Param("order")ApiOrderListForm orderListForm,@Param("shopId")Integer shopId);

    @Select("<script>" +
            "select orderNo,realFee,payTime,source,is_meals,is_warm from binguo_order " +
            "where payTime > DATE_SUB(NOW(), INTERVAL 60 MINUTE) and customerId in " +
            "<foreach item='item' index='index' collection='customerIds' open='(' separator=',' close=')'> #{item.id} </foreach> " +
            "</script>")
    List<BinguoOrder> findOrdersByCustomer(@Param("customerIds")List<Customer> customerIds);

    Long sumOrder(@Param("startTime")String startTime, @Param("endTime")String endTime);

    List<TecentVO> selectOrderHourSum(@Param("startTime")String startTime,
                                      @Param("endTime")String endTime);

    List<TecentVO> selectOrderDaySum(@Param("startTime")String startTime,
                                     @Param("endTime")String endTime);

    List<TecentVO> selectCountBySource(@Param("startTime")String startTime,
                                       @Param("endTime")String endTime);

    List<BinguoOrder> selectOrderByCustomerIDDate(@Param("customerId")Integer customerId);

    Integer countOrder(@Param("startTime")String startTime,
                       @Param("endTime")String endTime);

    List<ReserveOrderVO> sumReserveOrder(@Param("startTime")String startTime,
                                         @Param("endTime")String endTime,
                                         @Param("branchId")Integer branchId);

    List<BinguoOrder> selectByOrderNoList(@Param("details")List<String> orderNo);

    List<BinguoOrderVO> findIsMealByOrderNos(@Param("orderNos") List<String> orderNos);
}